package com.boom.springapp.javafxdemo.common;

import java.util.List;
import java.util.Map;

import lombok.Data;

import java.util.ArrayList;

@Data
public class Comment {
    private String uname = ""; // 用户名
    private String sex = ""; // 性别
    private String content = ""; // 评论内容
    private long rpid = 0; // 评论ID
    private long oid = 0; // 评论区ID
    private String bvid = ""; // 视频BV号
    private long mid = 0; // 发送者ID
    private long parent = 0; // 父评论ID
    private int fansgrade = 0; // 是否粉丝
    private long ctime = 0; // 评论时间戳
    private int like = 0; // 点赞数
    private boolean following = false; // 是否关注
    private int currentLevel = 0; // 当前等级
    private String location = ""; // 位置
    private List<Picture> pictures = new ArrayList<>(); // 图片列表

    // 构造函数
    public Comment() {
    }

    // 带参数的构造函数
    public Comment(String uname, String sex, String content, long rpid, long oid,
            String bvid, long mid, long parent, int fansgrade, long ctime,
            int like, boolean following, int currentLevel, String location,
            List<Picture> pictures) {
        this.uname = uname;
        this.sex = sex;
        this.content = content;
        this.rpid = rpid;
        this.oid = oid;
        this.bvid = bvid;
        this.mid = mid;
        this.parent = parent;
        this.fansgrade = fansgrade;
        this.ctime = ctime;
        this.like = like;
        this.following = following;
        this.currentLevel = currentLevel;
        this.location = location;
        this.pictures = pictures != null ? pictures : new ArrayList<>();
    }

    /**
     * 从API响应创建Comment对象
     * 
     * @param item API响应数据项
     * @return Comment对象
     */
    public static Comment fromApiResponse(Map<String, Object> item) {
        // 处理图片列表
        List<Picture> pictures = new ArrayList<>();
        if (item.containsKey("content")) {
            Map<String, Object> content = (Map<String, Object>) item.get("content");
            if (content.containsKey("pictures")) {
                List<Map<String, Object>> pictureList = (List<Map<String, Object>>) content.get("pictures");
                for (Map<String, Object> pic : pictureList) {
                    pictures.add(new Picture((String) pic.get("img_src")));
                }
            }
        }

        // 创建并返回Comment对象
        Comment comment = new Comment();
        comment.setUname(getNestedString(item, "member", "uname", ""));
        comment.setSex(getNestedString(item, "member", "sex", ""));
        comment.setContent(getNestedString(item, "content", "message", ""));
        comment.setRpid(getNestedLong(item, "rpid", 0L));
        comment.setOid(getNestedLong(item, "oid", 0L));
        comment.setBvid(""); // 需要在外部设置
        comment.setMid(getNestedLong(item, "mid", 0L));
        comment.setParent(getNestedLong(item, "parent", 0L));
        comment.setFansgrade(getNestedInt(item, "fansgrade", 0));
        comment.setCtime(getNestedLong(item, "ctime", 0L));
        comment.setLike(getNestedInt(item, "like", 0));

        // 处理following字段
        boolean following = false;
        if (item.containsKey("reply_control")) {
            Map<String, Object> replyControl = (Map<String, Object>) item.get("reply_control");
            if (replyControl.containsKey("following")) {
                following = (Boolean) replyControl.get("following");
            }
        }
        comment.setFollowing(following);

        // 处理current_level字段
        int currentLevel = 0;
        if (item.containsKey("member")) {
            Map<String, Object> member = (Map<String, Object>) item.get("member");
            if (member.containsKey("level_info")) {
                Map<String, Object> levelInfo = (Map<String, Object>) member.get("level_info");
                if (levelInfo.containsKey("current_level")) {
                    currentLevel = ((Number) levelInfo.get("current_level")).intValue();
                }
            }
        }
        comment.setCurrentLevel(currentLevel);

        // 处理location字段
        String location = "";
        if (item.containsKey("reply_control")) {
            Map<String, Object> replyControl = (Map<String, Object>) item.get("reply_control");
            if (replyControl.containsKey("location")) {
                location = (String) replyControl.get("location");
                location = location.replace("IP属地：", "");
            }
        }
        comment.setLocation(location);

        comment.setPictures(pictures);

        return comment;
    }

    // 辅助方法：获取嵌套的字符串值
    private static String getNestedString(Map<String, Object> map, String key1, String key2, String defaultValue) {
        if (map.containsKey(key1)) {
            Map<String, Object> nested = (Map<String, Object>) map.get(key1);
            if (nested.containsKey(key2)) {
                Object value = nested.get(key2);
                return value != null ? value.toString() : defaultValue;
            }
        }
        return defaultValue;
    }

    // 辅助方法：获取嵌套的长整型值
    private static long getNestedLong(Map<String, Object> map, String key, long defaultValue) {
        if (map.containsKey(key)) {
            Object value = map.get(key);
            if (value instanceof Number) {
                return ((Number) value).longValue();
            } else if (value != null) {
                try {
                    return Long.parseLong(value.toString());
                } catch (NumberFormatException e) {
                    return defaultValue;
                }
            }
        }
        return defaultValue;
    }

    // 辅助方法：获取嵌套的整型值
    private static int getNestedInt(Map<String, Object> map, String key, int defaultValue) {
        if (map.containsKey(key)) {
            Object value = map.get(key);
            if (value instanceof Number) {
                return ((Number) value).intValue();
            } else if (value != null) {
                try {
                    return Integer.parseInt(value.toString());
                } catch (NumberFormatException e) {
                    return defaultValue;
                }
            }
        }
        return defaultValue;
    }

}
